package com.delong.service.market;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.delong.dao.DaoHelper;
import com.delong.dao.market.PicturesMapper;
import com.delong.model.market.Pictures;

public class PicturesService {
	public void addElement(Pictures element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			PicturesMapper mapper = session.getMapper(PicturesMapper.class);
			mapper.addElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void updateElement(Pictures element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			PicturesMapper mapper = session.getMapper(PicturesMapper.class);
			mapper.updateElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void deleteElementByIds(List<Integer> ids) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			PicturesMapper mapper = session.getMapper(PicturesMapper.class);
			mapper.deleteElementsByIds(ids);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void updateStatus(int id, int status) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			PicturesMapper mapper = session.getMapper(PicturesMapper.class);
			mapper.changeStatus(id, status);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public List<Pictures> getElementsByIds(List<Integer> ids) {
		SqlSession session = null;
		List<Pictures> result = new ArrayList<Pictures>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			PicturesMapper mapper = session.getMapper(PicturesMapper.class);
			result = mapper.selectElementsByIds(ids);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public List<Pictures> selectElementBanner(int pageNo,int limit) {
		SqlSession session = null;
		List<Pictures> result = new ArrayList<Pictures>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			PicturesMapper mapper = session.getMapper(PicturesMapper.class);
			result = mapper.selectElementBanner(pageNo,limit);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}
	
	public int selectElementBannerCount()
	{
		SqlSession session = null;
		int result = 0;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			PicturesMapper mapper = session.getMapper(PicturesMapper.class);
			result = mapper.selectElementBannerCount();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

}
